package com.ewanghuiju.app.ui.web;

import android.os.Bundle;

import com.ewanghuiju.app.base.SimpleActivity;

public abstract class WebViewActivity extends SimpleActivity {
    public MyWebViewClient x5WebViewClient;

    @Override
    protected void initEventAndData() {
        x5WebViewClient = findViewById(getViewId());
        initView();
        initEvent();
        initData();
    }

    protected abstract int getViewId();

    protected abstract void initView();

    public boolean isNotNull() {
        if (x5WebViewClient != null && x5WebViewClient.getWebView() != null) {
            return true;
        }
        return false;
    }

    public void onChangeTitle(String title) {
    }

    public void pageFinish() {

    }

    protected void initEvent() {
        x5WebViewClient.setOnPageListener(new X5WebView.OnPageListener() {


            @Override
            public void onPageFinish() {
                pageFinish();
            }

            @Override
            public void onTitleChange(String title) {
                onChangeTitle(title);
            }
        });
        if (isNotNull() &&
                x5WebViewClient.getWebView().getX5WebViewExtension() != null) {
            Bundle data = new Bundle();
            data.putBoolean("standardFullScreen", false);
            //true表示标准全屏，false表示X5全屏；不设置默认false，
            data.putBoolean("supportLiteWnd", true);
            //false：关闭小窗；true：开启小窗；不设置默认true，
            data.putInt("DefaultVideoScreen", 1);
            //1：以页面内开始播放，2：以全屏开始播放；不设置默认：1
            x5WebViewClient.getWebView().getX5WebViewExtension().invokeMiscMethod("setVideoParams", data);

        }
        doSomeThing();

    }


    public abstract void initData();

    public void loadUrl(String url) {

        if (isNotNull()) {
            x5WebViewClient.loadUrl(url);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (isNotNull()) {
            x5WebViewClient.getWebView().destroy();
        }

    }

    @Override
    public void onBackPressedSupport() {
        if (!canBack()) {
            super.onBackPressedSupport();
        }

    }

    public boolean canBack() {
        return canBack(true);
    }

    public boolean canBack(boolean back) {
        if (isNotNull() && x5WebViewClient.getWebView().canGoBack()) {
            if (back) {
                x5WebViewClient.getWebView().goBack();
            }
            return true;
        }
        return false;
    }

    public void doSomeThing() {

    }


}
